<?php

namespace Modules\Admin\Http\Controllers;

use App\Admin;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Http\Requests\AdminUserRequest;
use Modules\Admin\Http\Requests\PasswordRequest;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Hash;
class AdminUserController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$admins=Admin::where('name','<>','webmaster')->paginate(8);
        return view('admin::adminuser.index',compact('admins'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(AdminUserRequest $request)
    {
    	if($request->input('password')){
    		$post=$request->all();
    		$post['password']=bcrypt($request->input('password'));
			Admin::create($post);
			//session()->flash('success','添加用户成功！');
			Flash('添加用户成功！')->overlay ();
			return redirect ()->back();
		}
    	session()->flash ('danger','密码不能为空');
    	return redirect ()->back();

    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(AdminUserRequest $request,$id)
    {
    	$admin=Admin::find($id);
    	$post=$request->all();
    	if($post['password']){
    		$admin->password=bcrypt($post['password']);
		}
		$admin->name=$post['name'];
    	$admin->nickname=$post['nickname'];
    	$admin->save();
    	session()->flash('success','修改用户信息成功！');
    	return redirect ()->back();
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
    	$admin=Admin::find($id);
    	if($admin->name=='webmaster'){
    		session()->flash('danger','小伙子，你的思想很危险！');
    		return redirect ()->back();
		}
    	Admin::destroy ($id);
    	session()->flash('success','删除用户成功！');
    	return redirect ()->back();
    }
    public function showrole($id){
    	$admin=Admin::find($id);
		$roles=Role::get();
		return view('admin::role.showrole',compact('roles','admin'));
	}
	public function addrole(Request $request,$id){
    	if(!$request->input('name')){
    		session()->flash('success','请至少选择一个角色！');
    		return redirect ()->back();
		}
		$admin=Admin::find($id);
    	if($admin['name']=='webmaster'){
			session()->flash('danger','小伙子，你的思想很危险！居然敢修改站长的角色');
			return redirect ()->back();
		}
		$admin->syncRoles($request->input('name'));
		session()->flash('success','分配角色成功！');
		return redirect ()->route('adminuser.index');
	}
	public function showPasswordForm(){
    	return view('admin::password.show');
	}
	public function resetPassword(PasswordRequest $request,$id){
		if(!$id==\Auth::user ()->id){
			session()->flash('danger','您没有修改他人密码的权利！');
			return redirect ()->back();
		}
		if(!Hash::check ($request->input('oldpassword'),Auth()->user ()->password)){
			session()->flash('danger','原密码不正确');
			return redirect ()->back();
		}
		$user=\Auth::user ();
		$user->password=bcrypt($request->input('password'));
		$user->save();
		session()->flash('success','密码修改成功！');
		return redirect ('/admin');
	}
}
